<?php
namespace Biz\model;

use Doraemon\pockets\datebase\BaseDbModel;

class GoodsPriceLog extends BaseDbModel
{

	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_shop';

	const _DATABASE_ = 'shop';

	const _TABLE_NAME = 'goods_price_log';


	const LOG_TYPE_NORMAL = 1; //普通商品价格

	const LOG_TYPE_LIMIT_TIME_ROBBING = 2; //限时抢商品价格

	/**
	 * @param $goods_id
	 * @param array $sku_price
	 * [
	 *   [
	 *      'sku_id' => 1,
	 *      'sku_name' => 红色 xl,
	 *      'price' => 4.00
	 *   ]
	 * ]
	 * @param int $log_type
	 * @return mixed
	 */
	public static function addLog($goods_id, array $skus_info, $log_type = self::LOG_TYPE_NORMAL)
	{
		$data = [];

		array_walk($skus_info, function($sku) use ($goods_id, $log_type, &$data) {
			if (is_array($sku) && $sku) $data[] = [
				'goods_id' => $goods_id,
				'sku_id' => $sku['sku_id'],
				'sku_name' => $sku['sku_name'],
				'type' => $log_type,
				'price' => $sku['price'],
				'add_time' => date("Y-m-d H:i:s")
			];
		});

		return $data ?  self::insert($data) : null;
	}

	public static function is_can_add($goods_id, $sku_name, $price, $log_type = self::LOG_TYPE_NORMAL)
	{
		$pre_price = self::get('price', [
			'goods_id' => $goods_id,
			'sku_name' => $sku_name,
			'type' => $log_type,
			'ORDER' => ['add_time' => 'DESC']
		]);

		return $price == $pre_price ? false : true;

	}




}